//
//  UIView+Hud.m
//  Coffee
//
//  Created by lbh on 2017/8/12.
//  Copyright © 2017年 周天雨. All rights reserved.
//

#import "UIView+Hud.h"

static NSString *hudStatusKey = @"hudStatusKey"; //name的key

@implementation UIView (Hud)

- (void)setHudStatus:(NSString *)hudStatus{
    objc_setAssociatedObject(self, &hudStatusKey, hudStatus, OBJC_ASSOCIATION_COPY);
}
- (NSString *)hudStatus {
    return objc_getAssociatedObject(self, &hudStatusKey);
}
- (void)showHUD {
    dispatch_main_async_safe(^{
        if (![NSString isEmpty:self.hudStatus]) {
            return;
        }
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
        hud.bezelView.color = [UIColor whiteColor];
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
//        hud.backgroundColor = [UIColor redColor];
//        hud.backgroundView.backgroundColor = [UIColor blueColor];
        hud.label.text = @"";
        hud.label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
        hud.label.textAlignment = NSTextAlignmentCenter;
        hud.label.textColor = UIColor.blackColor;
        hud.userInteractionEnabled = YES;
        self.hudStatus = @"1";
    });
}
- (void)showBusyHUD {
    dispatch_main_async_safe(^{
        if (![NSString isEmpty:self.hudStatus]) {
            return;
        }
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
        hud.bezelView.color = [UIColor whiteColor];
        hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
        hud.label.text = @"加载中...";
        hud.label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
        hud.label.textAlignment = NSTextAlignmentCenter;
        hud.label.textColor = UIColor.blackColor;
        hud.userInteractionEnabled = YES;
        self.hudStatus = @"1";
    });
}
- (void)hideBusyHUD {
    dispatch_main_async_safe(^{
        self.hudStatus = nil;
        [MBProgressHUD hideHUDForView:self animated:YES];
    });
}

- (void)showWarning:(NSString *)warning onHide:(void (^)(void))onHide {
    dispatch_main_async_safe(^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.bezelView.color = RBG(0, 0, 0.7);
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.detailsLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
        hud.detailsLabel.text = warning;
        hud.detailsLabel.textColor = [UIColor whiteColor];
        [hud hideAnimated:YES afterDelay:1.5];
        if (onHide){
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                onHide();
            });
        }
        
    });
}
//绘制圆角
+ (CAShapeLayer *)createBezierPath:(CGRect)frame withRoundingCorners:(UIRectCorner)corners withRadid:(CGSize)sizeMake {
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:corners cornerRadii:sizeMake];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = frame;
    maskLayer.path = maskPath.CGPath;
    return maskLayer;
}
@end
